home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_UDELxntp.idb / usr / freeware / src / xntp-3.4o-export / authstuff / makePC2.c.z / makePC2.c
C/C++ Source or Header  |  1998-01-21  |  4KB  |  239 lines

  1. /*
  2.  * makePC2 - build custom permutted choice 2 tables
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7.  
  8. #include "ntp_stdlib.h"
  9.  
  10. #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  11.  
  12. char *progname;
  13. int debug;
  14.  
  15. static    void    permc    P((u_char *, u_long *));
  16. static    void    permd    P((u_char *, u_long *));
  17. static    void    doit    P((void));
  18.  
  19. /*
  20.  * main - parse arguments and handle options
  21.  */
  22. void
  23. main(argc, argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.     int c;
  28.     int errflg = 0;
  29.     extern int ntp_optind;
  30.     extern char *ntp_optarg;
  31.  
  32.     progname = argv[0];
  33.     while ((c = ntp_getopt(argc, argv, "d")) != EOF)
  34.         switch (c) {
  35.         case 'd':
  36.             ++debug;
  37.             break;
  38.         default:
  39.             errflg++;
  40.             break;
  41.         }
  42.     if (errflg) {
  43.         (void) fprintf(stderr, "usage: %s [-d]\n", progname);
  44.         exit(2);
  45.     }
  46.     doit();
  47.     exit(0);
  48. }
  49.  
  50. /*
  51.  * Permuted choice 2 table.  This actually produces the low order 24
  52.  * bits of the subkey Ki from the 28 bit value of Ci.  This has had
  53.  * 1 subtracted from it to give a zero base.
  54.  */
  55. static u_char PC2_C[24] = {
  56.     13, 16, 10, 23,  0,  4,
  57.      2, 27, 14,  5, 20,  9,
  58.     22, 18, 11,  3, 25,  7,
  59.     15,  6, 26, 19, 12,  1
  60. };
  61.  
  62. /*
  63.  * Permuted choice 2 table, operating on the 28 Di bits to produce the
  64.  * high order 24 bits of subkey Ki.  This has had 29 subtracted from
  65.  * it to give it a zero base into our D bit array.
  66.  */
  67. static u_char PC2_D[24] = {
  68.     12, 23,  2,  8, 18, 26,
  69.      1, 11, 22, 16,  4, 19,
  70.     15, 20, 10, 27,  5, 24,
  71.     17, 13, 21,  7,  0,  3
  72. };
  73.  
  74. u_long masks[4] = { 0x40000000, 0x400000, 0x4000, 0x40 };
  75.  
  76.  
  77. /*
  78.  * permc - permute C, producing a four byte result
  79.  */
  80. static void
  81. permc(bits, resp)
  82.     u_char *bits;
  83.     u_long *resp;
  84. {
  85.     register int part;
  86.     register int i;
  87.     register u_long mask;
  88.     u_char res[24];
  89.  
  90.     memset((char *)res, 0, sizeof res);
  91.  
  92.     for (i = 0; i < 24; i++) {
  93.         res[i] = bits[PC2_C[i]];
  94.     }
  95.  
  96.     *resp = 0;
  97.     for (part = 0; part < 4; part++) {
  98.         mask = masks[part];
  99.         for (i = part*6; i < (part+1)*6; i++) {
  100.             mask >>= 1;
  101.             if (res[i])
  102.                 *resp |= mask;
  103.         }
  104.     }
  105. }
  106.  
  107. /*
  108.  * permd - permute D, producing a four byte result
  109.  */
  110. static void
  111. permd(bits, resp)
  112.     u_char *bits;
  113.     u_long *resp;
  114. {
  115.     register int part;
  116.     register int i;
  117.     register u_long mask;
  118.     u_char res[24];
  119.  
  120.     memset((char *)res, 0, sizeof res);
  121.  
  122.     for (i = 0; i < 24; i++) {
  123.         res[i] = bits[PC2_D[i]];
  124.     }
  125.  
  126.     *resp = 0;
  127.     for (part = 0; part < 4; part++) {
  128.         mask = masks[part];
  129.         for (i = part*6; i < (part+1)*6; i++) {
  130.             mask >>= 1;
  131.             if (res[i])
  132.                 *resp |= mask;
  133.         }
  134.     }
  135. }
  136.  
  137.  
  138. /*
  139.  * bits used for each round in C
  140.  */
  141. static    int    cbits[4][6] = {
  142.     0, 1, 2, 3, 4, 5,
  143.     6, 7, 9, 10, 11, 12,
  144.     13, 14, 15, 16, 22, 23,
  145.     18, 19, 20, 25, 26, 27
  146. };
  147.  
  148.  
  149. /*
  150.  * bits used for each round in D
  151.  */
  152. static    int    dbits[4][6] = {
  153.     0, 1, 2, 3, 4, 5,
  154.     7, 8, 10, 11, 12, 13,
  155.     15, 16, 17, 18, 19, 20,
  156.     21, 22, 23, 24, 26, 27
  157. };
  158.  
  159.  
  160. /*
  161.  * doit - compute and print the four PC1 tables
  162.  */
  163. static void
  164. doit()
  165. {
  166.     int i;
  167.     int comb;
  168.     u_long res;
  169.     u_char bits[28];
  170.  
  171.     memset((char *)bits, 0, sizeof bits);
  172.  
  173.     printf("static u_long PC2_C[4][64] = {");
  174.     for (i = 0; i < 4; i++) {
  175.         for (comb = 0; comb < 64; comb++) {
  176.             if (comb & 0x20)
  177.                 bits[cbits[i][0]] = 1;
  178.             if (comb & 0x10)
  179.                 bits[cbits[i][1]] = 1;
  180.             if (comb & 0x8)
  181.                 bits[cbits[i][2]] = 1;
  182.             if (comb & 0x4)
  183.                 bits[cbits[i][3]] = 1;
  184.             if (comb & 0x2)
  185.                 bits[cbits[i][4]] = 1;
  186.             if (comb & 0x1)
  187.                 bits[cbits[i][5]] = 1;
  188.             permc(bits, &res);
  189.             bits[cbits[i][0]] = 0;
  190.             bits[cbits[i][1]] = 0;
  191.             bits[cbits[i][2]] = 0;
  192.             bits[cbits[i][3]] = 0;
  193.             bits[cbits[i][4]] = 0;
  194.             bits[cbits[i][5]] = 0;
  195.             if ((comb & 0x3) == 0)
  196.                 printf("\n\t0x%08x,", res);
  197.             else if (comb == 63 && i == 3)
  198.                 printf(" 0x%08x\n};\n\n", res);
  199.             else if (comb == 63)
  200.                 printf(" 0x%08x,\n", res);
  201.             else
  202.                 printf(" 0x%08x,", res);
  203.         }
  204.     }
  205.  
  206.     printf("static u_long PC2_D[4][64] = {");
  207.     for (i = 0; i < 4; i++) {
  208.         for (comb = 0; comb < 64; comb++) {
  209.             if (comb & 0x20)
  210.                 bits[dbits[i][0]] = 1;
  211.             if (comb & 0x10)
  212.                 bits[dbits[i][1]] = 1;
  213.             if (comb & 0x8)
  214.                 bits[dbits[i][2]] = 1;
  215.             if (comb & 0x4)
  216.                 bits[dbits[i][3]] = 1;
  217.             if (comb & 0x2)
  218.                 bits[dbits[i][4]] = 1;
  219.             if (comb & 0x1)
  220.                 bits[dbits[i][5]] = 1;
  221.             permd(bits, &res);
  222.             bits[dbits[i][0]] = 0;
  223.             bits[dbits[i][1]] = 0;
  224.             bits[dbits[i][2]] = 0;
  225.             bits[dbits[i][3]] = 0;
  226.             bits[dbits[i][4]] = 0;
  227.             bits[dbits[i][5]] = 0;
  228.             if ((comb & 0x3) == 0)
  229.                 printf("\n\t0x%08x,", res);
  230.             else if (comb == 63 && i == 3)
  231.                 printf(" 0x%08x\n};\n\n", res);
  232.             else if (comb == 63)
  233.                 printf(" 0x%08x,\n", res);
  234.             else
  235.                 printf(" 0x%08x,", res);
  236.         }
  237.     }
  238. }
  239.